package com.firefly.service.command.ums;

import com.firefly.dao.dao.ums.po.AdminPo;
import com.firefly.dao.util.RedisUtils;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.assembler.ums.AdminAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.ums.*;
import com.firefly.service.repository.ums.AdminRepository;
import com.firefly.service.repository.ums.DepartmentAdminRepository;
import com.firefly.service.repository.ums.RoleAdminRepository;
import com.firefly.util.utils.FuncUtils;
import com.firefly.util.utils.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @description: 管理员
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/9
 **/
@Service
public class AdminCommand {
    private final AdminAssembler adminAssembler;
    private final AdminRepository adminRepository;
    private final RoleAdminRepository roleAdminRepository;
    private final DepartmentAdminRepository departmentAdminRepository;
    private final RedisUtils redisUtils;
    @Autowired
    public AdminCommand(AdminAssembler adminAssembler,AdminRepository adminRepository,RoleAdminRepository roleAdminRepository,DepartmentAdminRepository departmentAdminRepository,RedisUtils redisUtils){
        this.adminAssembler = adminAssembler;
        this.adminRepository = adminRepository;
        this.roleAdminRepository = roleAdminRepository;
        this.departmentAdminRepository = departmentAdminRepository;
        this.redisUtils = redisUtils;
    }
    //	插入单条
    @Transactional
    public Boolean create(AdminCreateDto request) throws Exception {
        // 密码是否相等
        String pwd = request.getPassword();
        String pwd2 = request.getRepassword();
        if (!pwd.equals(pwd2)) {
            throw new FireflyException(400, "两次密码不同");
        }
        AdminPo entity = adminAssembler.getPo(request);
        Boolean result = adminRepository.create(entity);
        // 更新角色
        List<Integer> roleIds = request.getRoleIds();
        Integer id = entity.getId();
        roleAdminRepository.saveBatchByAdminId(id,roleIds);
        // 更新部门
        List<Integer> departmentIds = request.getDepartmentIds();
        departmentAdminRepository.saveBatchByAdminId(id,departmentIds);
        return result;
    }

    //	更新单条
    @Transactional
    public Boolean updateadmin(AdminUpdateDto request) {
        AdminPo entity = adminAssembler.getPo(request);
        Boolean result = adminRepository.updateInfo(entity);
        Integer adminId = request.getId();
        // 删除关联的信息
        roleAdminRepository.removeByAdminId(adminId);
        // 更新角色
        List<Integer> roleIds = request.getRoleIds();
        roleAdminRepository.saveBatchByAdminId(adminId,roleIds);
        // 删除关联的信息
        departmentAdminRepository.removeByAdminId(adminId);
        // 更新部门
        List<Integer> departmentIds = request.getDepartmentIds();
        departmentAdminRepository.saveBatchByAdminId(adminId,departmentIds);
        return result;
    }

    //	更新密码
    public Boolean updatepwd(AdminPwdDto request){
        // 密码是否相等
        String pwd = request.getPassword();
        String pwd2 = request.getRepassword();
        if (!pwd.equals(pwd2)) {
            throw new FireflyException(400, "两次密码不同");
        }
        // 加密，秘钥
        Map<String,String> pwdMap = FuncUtils.getPassword(request.getPassword(),10);
        Boolean result = adminRepository.updatePwd(request.getId(),pwdMap.get("crypt"),pwdMap.get("pwd"));
        // 清除登录状态
        redisUtils.del("c-token:" + request.getId());
        return result;
    }

    //	更新单条状态
    public Boolean updatestatus(AdminStatusDto request) {
        if (request.getId().equals(1)){
            return true;
        }
        Boolean result = adminRepository.updateStatus(request.getId(),request.getStatus());
        // 清除登录状态
        redisUtils.del("c-token:" + request.getId());
        return result;
    }


    // 修改个人资料
    public Boolean updateSelf(AdminUpdateSelfDto request,Integer userId) throws JsonProcessingException {
        AdminPo entity = adminAssembler.getPo(userId,request);
        return adminRepository.updateInfo(entity);
    }

    //	修改密码密码
    public Boolean updateSelfPwd(AdminPwdDto request,Integer userId) throws Exception {
        // 密码是否相等
        String pwd = request.getPassword();
        String pwd2 = request.getRepassword();
        if (!pwd.equals(pwd2)) {
            throw new FireflyException(400, "两次密码不同");
        }
        // 加密，秘钥
        Map<String,String> pwdMap = FuncUtils.getPassword(request.getPassword(),10);
        Boolean result = adminRepository.updatePwd(userId,pwdMap.get("crypt"),pwdMap.get("pwd"));
        // 清除登录状态
        redisUtils.del("c-token:" + request.getId());
        return result;
    }
    //	删除单条
    @Transactional
    public Boolean remove(CommonDto request) {
        int adminId = request.detailId;
        // 判断是不是创始人
        if (adminId == 1) {
            throw new FireflyException(403, "创始人不能删除");
        }
        // 删除关联的信息
        roleAdminRepository.removeByAdminId(adminId);
        departmentAdminRepository.removeByAdminId(adminId);
        Boolean result = adminRepository.removeById(adminId);
        // 清除登录状态
        redisUtils.del("c-token:" + adminId);
        return result;
    }

    @Transactional
    public Boolean removeMultiple(CommonDto request){
        List<Integer> adminIds = request.detailIds;
        // 判断是不是创始人
        if (adminIds.contains(1)) {
            throw new FireflyException(403, "创始人不能删除");
        }
        // 同时删除关联的权限信息
        roleAdminRepository.removeByAdminIds(adminIds);
        departmentAdminRepository.removeByAdminIds(adminIds);
        Boolean result = adminRepository.removeByIds(adminIds);
        // 清除登录状态
        adminIds.forEach(item -> redisUtils.del("c-token:" + item));
        return result;
    }
}
